package com.zhb.service;

import com.zhb.beans.SourcesPhoto;
import com.zhb.mapper.SourcesPhotoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @program: SourceAPI
 * @description: 文件上传
 * @author: rainboz
 * @create: 2019-10-24 12:09
 **/
@Service
public class SourcesPhotoServiceImpl implements SourcesPhotoService {

    @Autowired
    private SourcesPhotoMapper sourcesPhotoMapper;

    private String filePath = "G:\\img\\"; //定义上传文件的存放位置


    @Override
    public void upLoadFile(MultipartFile upload) {
        String fileName = upload.getOriginalFilename();  //获取上传文件的名字
        //判断文件夹是否存在,不存在则创建
        File file = new File(filePath);

        if (!file.exists()) {
            file.mkdirs();
        }

        String newFilePath = filePath + fileName; //新文件的路径

        try {
            upload.transferTo(new File(newFilePath));  //将传来的文件写入新建的文件
            System.out.println("文件路径: " + newFilePath);

        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
    }

    @Transactional
    public void save(MultipartFile file, SourcesPhoto sourcesPhoto) {
        // 保存图片的路径，图片上传成功后，将路径保存到数据库
        String filePath = "G:\\upload";
        // 获取原始图片的扩展名
        String originalFilename = file.getOriginalFilename();
        String extensionName = originalFilename.substring(originalFilename.lastIndexOf("."));

        //判断文件夹是否存在,不存在则创建
        File exit_file = new File(filePath);

        if (!exit_file.exists()) {
            exit_file.mkdirs();
        }

        // 生成文件新的名字
//        String newFileName = UUID.randomUUID() + extensionName;
        String newFileName = originalFilename;
        long imgSize = file.getSize();
        String imgUrl = "/images/" + newFileName;

        // 封装上传文件位置的全路径
        File targetFile = new File(filePath, newFileName);
        try {
            file.transferTo(targetFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());

        // 保存到数据库
        sourcesPhoto.setImgName(newFileName);
        sourcesPhoto.setImgSize(imgSize);
        sourcesPhoto.setImgUrl(imgUrl);
        sourcesPhoto.setGmtCreate(format);
        sourcesPhoto.setGmtModified(format);
        int b = sourcesPhotoMapper.save(sourcesPhoto);
        System.out.println("是否成功：" + b);
        System.out.println("图片信息：" + sourcesPhoto);
        System.out.println("filePath：" + filePath);
        System.out.println("originalFilename: " + originalFilename);
        System.out.println("扩展名：" + extensionName);
    }

    @Override
    public void saveImg(MultipartFile file, SourcesPhoto sourcesPhoto) {
        sourcesPhotoMapper.saveImg(sourcesPhoto);
    }

    @Transactional(readOnly = true)
    public List<SourcesPhoto> getImgList() {
        return sourcesPhotoMapper.getImgList();
    }

    @Override
    public boolean selectPohtoByName(String imgName) {
        boolean flag = false;
        SourcesPhoto sourcesPhoto = sourcesPhotoMapper.selectPohtoByName(imgName);
        if (sourcesPhoto == null){
            //图片可上传
            flag = true;
        }else {
            //图片已存在
        }
        return flag;
    }


    @Transactional(readOnly = true)
    public List<SourcesPhoto> pageQuery(Map<String, Object> map) {
        return sourcesPhotoMapper.pageQueryData(map);
    }

    @Transactional(readOnly = true)
    public int pageQueryCount(Map<String, Object> map) {
        return sourcesPhotoMapper.pageQueryCount(map);
    }
}
